package com.example.yls.demoa;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.btn_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showNameDialog();
            }

            private void showNameDialog() {
                final EditText editText1 = new EditText(MainActivity.this);
                final EditText editText2 = new EditText(MainActivity.this);
                final View view = LayoutInflater.from(MainActivity.this)
                        .inflate(R.layout.activity_dialog_input, null, false);

                final EditText nameEdit = view.findViewById(R.id.edt_name);
                final EditText ageEdit = view.findViewById(R.id.edt_age);

                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请输入你的信息")
                        .setView(view)

                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String name = nameEdit.getText().toString();
                                String age = ageEdit.getText().toString();

                                Toast.makeText(MainActivity.this,
                                        "你的姓名是" + name + "你的年龄是" + age, Toast.LENGTH_SHORT).show();


                            }
                        }).show();

            }

        });


        findViewById(R.id.btn_dialog1).setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                showSimpleDialog();
            }
        });
        findViewById(R.id.btn_hobby).setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                showHobbyDialog();
            }

            private void showHobbyDialog() {
                final String[] hobby = {"篮球", "足球", "棒球", "睡觉", "乒乓球"};
                final boolean isChecked[] = new boolean[6];
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择你的爱好")
                        .setMultiChoiceItems(hobby, null,
                                new DialogInterface.OnMultiChoiceClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                        isChecked[i] = b;

                                        if (b) {
                                            Toast.makeText(MainActivity.this,
                                                    "你添加了" + hobby[i], Toast.LENGTH_SHORT).show();
                                        } else {
                                            Toast.makeText(MainActivity.this,
                                                    "你取消了" + hobby[i], Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                StringBuffer buffer = new StringBuffer();

                                for (int j = 0; j < isChecked.length; j++) {
                                    if (isChecked[j]) {
                                        buffer.append(hobby[j]);
                                    }
                                }
                                Toast.makeText(MainActivity.this, "你的爱好有"
                                        + buffer, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();

            }
        });


        findViewById(R.id.btn_sex).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSexDialog();
            }


            private void showSexDialog() {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher_background);
                builder.setTitle("请选择性别");
                final String[] sex = {"男", "女", "未知性别"};
                builder.setSingleChoiceItems(sex, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,
                                "性别为：" + sex[which],
                                Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });

        findViewById(R.id.btn_adress).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showWhereDialog();
            }

            public void showWhereDialog() {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher_background);
                builder.setTitle("请选择你居住的城市");
                final String[] cities = {"广州", "茂名", "化州", "潭塘", "拉萨"};
                builder.setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "选择的城市为：" + cities[i],
                                Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();


            }
        });

        findViewById(R.id.btn_deleted).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showChooseDialog();
            }
        });


    }


    public void showChooseDialog() {

        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("弹出警告框")
                .setMessage("确定要删除吗")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "后会有期", Toast.LENGTH_SHORT).show();
                    }
                })


                .setNegativeButton("留下", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "真聪明", Toast.LENGTH_SHORT).show();
                    }
                })


                .setNeutralButton("不知道", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,
                                "犹豫不决", Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
//        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//        builder.setIcon(R.drawable.ic_launcher_background);
//        builder.setTitle("弹出警告框");
//        builder.setMessage("确定要删除吗？");
//
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int which) {
//                Toast.makeText(MainActivity.this,
//                        "positive: " + which,
//                        Toast.LENGTH_SHORT).show();
//            }
//        });
//
//        builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int which) {
//                Toast.makeText(MainActivity.this,
//                        "neutral: " + which,
//                        Toast.LENGTH_SHORT).show();
//            }
//        });
//
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int which) {
//                Toast.makeText(MainActivity.this,
//                        "negative: " + which,
//                        Toast.LENGTH_SHORT).show();
//            }
//        });
//        builder.show();
    }

    private void showSimpleDialog() {


        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("提示")
                .setMessage("今天是十月二十四号")

                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                    }
                })

                .create()
                .show();


    }

}
